<?php

use Core\AdminController;

use Models\PurchaseRecord;
use Models\Item;
use Models\Product;

class PurchaseRecordController extends AdminController
{

	public function index()
	{
		$purchaseRecord = new PurchaseRecord;
		$begin_time = $this->input->get('begin_time');
		$end_time = $this->input->get('end_time');
		$expire = $this->input->get('expire');
		$product_id = $this->input->get('product_id');
		$item_id = $this->input->get('item_id');
		$id = $this->input->get('id');
		$keyword = $this->input->get('keyword');
		if($id){
			$purchaseRecord->where('id',$id);
		}
		if($keyword){
			$item = (new Item)->where('barcode',$keyword)->get_one();
			if($item){
				$item_id = $item['id'];
			}
		}
		if($item_id){
			$purchaseRecord->where('item_id',$item_id);
		}

		if($product_id){
			$purchaseRecord->where('product_id',$product_id);
		}
		if($expire){
			$purchaseRecord->where('stock >',0);
			$purchaseRecord->where('expire <',time());
		}
		if($begin_time){
			$purchaseRecord->where('create_time >=',strtotime($begin_time));
		}
		if($end_time){
			$purchaseRecord->where('create_time <=',strtotime($end_time));
		}


		$page = $purchaseRecord->order_by('id desc')->with('product,item')->paginate(20);
		
		$this->load->helper('form');
		return $this->load->view('admin/purchase_record/index', [
			'page' => $page
		]);
	}

	public function search(){
		$code = $this->input->get('code');
		$item = new Item();
		$code_item = $item->where('barcode',$code)->get_one();
		if($code_item){
			$product = new Product();
			$product = $product->find($code_item['product_id']);
			$data = $code_item->to_array();
			$data['p_title'] = $product['title'];
			$data['p_en_title'] = $product['en_title'];
			return $this->response($data);
		}else{
			return $this->response(['error'=>'404']);
		}
	}

	public function create()
	{

		if ($this->input->is_ajax_request()) {
			
			$itm = $this->input->post('item');
			$items = $itm['product_id'];
			$this->db->trans_start();
			$totalStock = 0;
			foreach ($items as $i=>$item) {
				$purchase_record = new PurchaseRecord();
				$purchase_record['product_id'] = $itm['product_id'][$i];
				$purchase_record['item_id'] = $itm['item_id'][$i];
				$purchase_record['number'] = $purchase_record['stock'] = $itm['number'][$i];
				$purchase_record['price'] = $itm['price'][$i];
				$purchase_record['expire'] = $itm['expire'][$i]?strtotime($itm['expire'][$i]):time()+3600*24*200;
				$purchase_record['remark'] = $itm['remark'][$i];

				$purchase_record['create_time'] = time();
				$purchase_record->add();

				$item = new Item();
				$item->increment('stock', $itm['item_id'][$i], $itm['number'][$i]);
				
				$product = new Product();
				$product->increment('stock', $itm['product_id'][$i], $itm['number'][$i]);
			}
			
			$this->db->trans_complete();

			if ($this->db->trans_status()) {
				return $this->response(['error' => 'ok']);
			} else {
				log_message('error', '保存进货记录失败'.json_encode($this->db->error()));
				return $this->response(null, 500);
			}
		}
		

		return $this->load->view('admin/purchase_record/create');
	}

	public function modify(){
		$id = $this->input->post('id');
		$PurchaseRecord = (new PurchaseRecord)->find($id);
		$PurchaseRecord['price'] = $this->input->post('price');
		$PurchaseRecord['number'] = $this->input->post('number');
		$PurchaseRecord['stock'] = $this->input->post('stock');
		$PurchaseRecord['expire'] = strtotime($this->input->post('expire'));
		$PurchaseRecord['remark'] = $this->input->post('remark');
		$PurchaseRecord->update();

		(new Item)->update($PurchaseRecord['item_id'],['stock'=>$this->input->post('item_stock')]);
		echo json_encode(array('code'=>0,'msg'=>'ok'));

	}

	public function detail($id){
		$PurchaseRecord = (new PurchaseRecord)->find($id);
		$PurchaseRecord['expire'] = date('Y-m-d',$PurchaseRecord['expire']);
		$item = (new Item)->find($PurchaseRecord['item_id']);
		$PurchaseRecord['item_stock'] = $item['stock'];
		echo json_encode($PurchaseRecord);
	}

	public function delete()
	{
		$id = intval($this->input->post('id'));
		$PurchaseRecord = new PurchaseRecord;
		$PurchaseRecord->delete($id);
		echo json_encode(array('code'=>0,'msg'=>'ok'));
	}
}